/******************************************************************
 * Created by: Goh Chee Hong                                    
 * Date of Creation:
 * Date of last modification: 31st/Jan/2009
 * Status: Finished.
 *                                                                
 * Description: allows a user to send an offline friend a message                              
 * ...
 ******************************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using Pbdms.Common.BaseClass;
namespace Pbdms.Forms.Tools
{
    public partial class FrmOfflineMsg : newForms
    {
        private Hashtable friends = new Hashtable();
        public FrmOfflineMsg()
        {
            InitializeComponent();
            Pbdms.Common.Global.paintBackGround(this);
        }
        /// <summary>
        /// add a friend function
        /// </summary>
        /// <param name="key"></param>
        public void addFriend(string key)
        {
            if (friends.Contains(key))
            {

            }
            else
            {
              
                    string id = BusinessLogic.DataController.BuddyController.create().selectIDBasedOnKeyIDFromDB(key);
                    Common.DataObject.Buddy buddy = BusinessLogic.DataController.BuddyController.create().selectABuddyBasedOnIDFromDB(id);
                    friends.Add(key, buddy.Name);
                    this.txUsers.Text = "";
                    foreach (string keyid in friends.Keys)
                    {
                        this.txUsers.Text = this.txUsers.Text + friends[keyid] + ",";
                    }
                    this.txUsers.Text = this.txUsers.Text.Substring(0, this.txUsers.Text.Length - 1);
            
                
            }
           
        }
        /// <summary>
        /// remove a friend
        /// </summary>
        /// <param name="key"></param>
        public void removeFriend(string key){
            if (friends.Contains(key))
            {
                friends.Remove(key);
                this.txUsers.Text = "";
                foreach (string keyid in friends.Keys)
                {
                    this.txUsers.Text = this.txUsers.Text + friends[keyid] + ",";
                }
                this.txUsers.Text = this.txUsers.Text.Substring(0, this.txUsers.Text.Length - 1);
            }
        }
        /// <summary>
        /// change name
        /// </summary>
        /// <param name="keyID"></param>
        /// <param name="oldName"></param>
        /// <param name="newName"></param>
        public override void changeName(string keyID, string oldName, string newName)
        {
            if (txUsers.Text.Trim().Equals(oldName))
            {
                txUsers.Text = newName;
                friends[keyID]=newName;
            }

        }
        /// <summary>
        /// attach a file
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAttach_Click(object sender, EventArgs e)
        {
            openFileDia.Title = "Please select a file";
            DialogResult result = openFileDia.ShowDialog();
            string filelocation = null;
            if (result == DialogResult.OK)
            {

                Pbdms.UserControls.FileInfoControl fileInfoControl = new Pbdms.UserControls.FileInfoControl();
                System.IO.FileInfo fileinfo = new System.IO.FileInfo(openFileDia.FileName);

                fileInfoControl.setFileName(fileinfo.Name);
                fileInfoControl.setSize(Common.Global.GetFileSize(fileinfo.Length));
             
                fileInfoControl.Dock = DockStyle.Top;
                
              
                fileInfoControl.LinkLabelClicked += new EventHandler(control_Remove); ;
                fileInfoControl.Tag = openFileDia.FileName;
                panelFileInfo.Controls.Add(fileInfoControl);
                
            }
            else
            {

            }
        }
        public void setSubject(string subject)
        {
            this.txSubject.Text = subject;
        }
        public void setText(string text)
        {
            this.richmsg.Rtf = text;
            
        }
        /// <summary>
        /// remove the file control
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void control_Remove(object sender, EventArgs e)
        {
            Pbdms.UserControls.FileInfoControl filecontrol = (Pbdms.UserControls.FileInfoControl)sender;
            panelFileInfo.Controls.Remove(filecontrol);
        }
        /// <summary>
        /// send function
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSend_Click(object sender, EventArgs e)
        {
            if (error() == true)
            {
                MessageBox.Show("Please fill in the subject and the message body");
            }
            else
            {
                
                BusinessLogic.Mail.Message offlineMsg = new BusinessLogic.Mail.Message();
                offlineMsg.subject = txSubject.Text;
                offlineMsg.message = richmsg.Rtf;
                offlineMsg.datetime = DateTime.Now;

                offlineMsg.secret = Common.Global.RandomString();
                offlineMsg.isRead = false;
                /*
                foreach (string key in friends.Keys)
                {
                    offlineMsg.receipients.Add(key);
                }
             */
                ArrayList attachments = new ArrayList();
                foreach (Control ctr in panelFileInfo.Controls)
                {
                    attachments.Add((string)ctr.Tag);
                }
                //add extra friends
                if (checkExists() == false)
                {
                    Pbdms.Forms.Views.FrmUploadingOfflineMessage frmUploadingMessage = new Pbdms.Forms.Views.FrmUploadingOfflineMessage();
                    frmUploadingMessage.MdiParent = this.MdiParent;
                    frmUploadingMessage.setParameters(friends, offlineMsg, attachments);
                    frmUploadingMessage.Show();
                }
                else
                {
                    MessageBox.Show("One active uploading process is allowed.");
                }
            }

        }
        /// <summary>
        /// check whether a uploading message exists
        /// </summary>
        /// <returns></returns>
        private Boolean checkExists()
        {
            Boolean alreadyExist = false;
            foreach (Form frm in (this.MdiParent.MdiChildren))
            {
                frm.WindowState = FormWindowState.Normal;
                if (frm.GetType().Equals(typeof(Pbdms.Forms.Views.FrmUploadingOfflineMessage)))
                {
                    Pbdms.Forms.Views.FrmUploadingOfflineMessage checkForm = (Pbdms.Forms.Views.FrmUploadingOfflineMessage)frm;
                    if (checkForm.finished())
                    {
                        checkForm.Close();
                        alreadyExist = false;
                    }
                    else
                    {
                        alreadyExist = true;
                    }
                    break;
                }
            }
            return alreadyExist;
        }
        private void FontToolStripButton_Click(object sender, EventArgs e)
        {

            if (FontDlg.ShowDialog() != System.Windows.Forms.DialogResult.Cancel)
            {
                
                richmsg.SelectionFont = FontDlg.Font;
            }
        }

        private void FontColorToolStripButton_Click(object sender, EventArgs e)
        {
            if (ColorDlg.ShowDialog() != System.Windows.Forms.DialogResult.Cancel)
            {
                richmsg.SelectionColor = ColorDlg.Color;
            }
        }

        private void BoldToolStripButton_Click(object sender, EventArgs e)
        {
            Font currentFont = richmsg.SelectionFont;
            FontStyle fs = currentFont.Style;
            if (richmsg.SelectionFont.Bold == true)
            {
                fs = fs & ~FontStyle.Bold;

            }
            else
            {
                fs = fs | FontStyle.Bold;
            }

            richmsg.SelectionFont =
              new Font(currentFont.FontFamily, currentFont.Size, fs);

            // Check/Uncheck Bold button
            BoldToolStripButton.Checked = (richmsg.SelectionFont.Bold ? true : false);
        }
        private Boolean error()
        {
            if (txSubject.Text.Trim().Equals(""))
            {
                return true;
            }
            if (richmsg.Text.Trim().Equals(""))
            {
                return true;
            }
            return false; ;
        }
        private void UnderlineToolStripButton_Click(object sender, EventArgs e)
        {
            Font currentFont = richmsg.SelectionFont;
            FontStyle fs = currentFont.Style;
            if (richmsg.SelectionFont.Underline == true)
            {
                fs = fs & ~FontStyle.Underline;

            }
            else
            {
                fs = fs | FontStyle.Underline;
            }

            richmsg.SelectionFont =
              new Font(currentFont.FontFamily, currentFont.Size, fs);

            // Check/Uncheck Bold button
            UnderlineToolStripButton.Checked = (richmsg.SelectionFont.Underline ? true : false);
        }
        /// <summary>
        /// cancel event handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}